package com.google.code.pathlet.eshop.widget.web;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.google.code.pathlet.config.anno.InstanceIn;
import com.google.code.pathlet.config.anno.SpaceIn;
import com.google.code.pathlet.core.InstanceSpace;
import com.google.code.pathlet.util.FileUtils;

public class FileUploadAction {
	
	private static Log log = LogFactory.getLog(FileUploadAction.class);
	
	@InstanceIn(path="/request")
	private HttpServletRequest request;

	public List<ResponseInfo> upload() throws Exception {
		
		System.out.println("~~~~~~~~~~~~~~~~~~~" + request.getContentType());
		
		List<ResponseInfo> infos = new ArrayList<ResponseInfo>();
		
		boolean isMultipart = ServletFileUpload.isMultipartContent(request);
		if(isMultipart == true) {
			log.info("Uploading file ...");

			long fileMaxSize = 5000000L;
			
			// Create a factory for disk-based file items
			DiskFileItemFactory factory = new DiskFileItemFactory();
	
			// Set factory constraints
			factory.setSizeThreshold(10240);
			
			//If ignore setRepository, the the system temporary directory will be set as default.
			//factory.setRepository(yourTempDirectory); 
	
			// Create a new file upload handler
			ServletFileUpload upload = new ServletFileUpload(factory);
	
			// Set overall request size constraint
			upload.setSizeMax(fileMaxSize);
	
			List<FileItem> items = upload.parseRequest(request);
			Iterator<FileItem> iter = items.iterator();
			while (iter.hasNext()) {
			    FileItem item = iter.next();
	
			    if (item.isFormField() == false) {
			    	//String fieldName = item.getFieldName();
			    	//boolean isInMemory = item.isInMemory();
			    	
			    	//item may return the absolute full path name. we only need the last name
			        String fileName = FileUtils.parseFileName(item.getName());
			        String contentType = item.getContentType();
			        long sizeInBytes = item.getSize();
			        
			        log.info("Uploading fileName=" + fileName + ", contentType=" + contentType + ", sizeInBytes=" + sizeInBytes);
			        
			        InputStream uploadedStream = item.getInputStream();
			        try {
			        	FileUtils.writeToFile(new File("D:\\DEV_ENV\\workspace\\pathlet\\pathlet-eshop\\main\\webapp\\" + fileName), uploadedStream);
			        }
			        finally {
			        	uploadedStream.close();
			        }
			        
			        ResponseInfo info = new ResponseInfo();
					info.setName(fileName);
					info.setUrl("/eshop/upload/" + fileName);
					infos.add(info);
			    }
			}
	
			
			log.info("Uploading completed ...");
		}

		return infos;
		
		
		//return new ResponseMessage(ResponseLevel.INFO, "Upload Succeeded!");
	}
	
	public static class ResponseInfo {
		private String name;
		private String size;
		private String url;
		private String thumbnail_url;
		private String delete_url;
		private String delete_type;
		
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public String getSize() {
			return size;
		}
		public void setSize(String size) {
			this.size = size;
		}
		public String getUrl() {
			return url;
		}
		public void setUrl(String url) {
			this.url = url;
		}
		public String getThumbnail_url() {
			return thumbnail_url;
		}
		public void setThumbnail_url(String thumbnail_url) {
			this.thumbnail_url = thumbnail_url;
		}
		public String getDelete_url() {
			return delete_url;
		}
		public void setDelete_url(String delete_url) {
			this.delete_url = delete_url;
		}
		public String getDelete_type() {
			return delete_type;
		}
		public void setDelete_type(String delete_type) {
			this.delete_type = delete_type;
		}
		
		
		
		
	}

}
